package com.jojo.jojoWorld.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.easyExcel.entity.User;
import com.easyExcel.listener.UserListener;
import com.jojo.jojoWorld.entity.Jojo;
import com.jojo.jojoWorld.mapper.JojoMapper;
import com.jojo.jojoWorld.service.JojoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * JojoControllrt
 *
 * @author byc
 * @date 2021-09-22
 */
@RestController
@RequestMapping("/world")
public class JojoController {

    @Resource
    private JojoService jojoService;
    @Resource
    private JojoMapper jojoMapper;

    @GetMapping("/hello")
    public String hello(){
        return "Hello";
    }

    @GetMapping("/all")
    public List<Jojo> findAll(){
        return jojoMapper.findAll();
    }
    @GetMapping("/one/{id}")
    public List<Jojo> findById(@PathVariable("id")int id){
        return jojoMapper.findById(id);
    }

    @GetMapping("/list")
    public List<Jojo> list(){
        return jojoService.findAll();
    }

    /**
     * 逻辑删除 自己写sql
     */
    @GetMapping("/del/{ids}")
    public String del(@PathVariable("ids")int[] ids){
        jojoService.delFlag(ids);
        return "删除成功";
    }

    /**
     * 逻辑删除 plus加注解加配置自带方法
     */
    @GetMapping("/deli/{id}")
    public String deli(@PathVariable("id") int id){
        jojoService.del(id);
        return "删除成功";
    }

    @GetMapping("/allInfo/{id}")
    public List<Jojo> allInfo(@PathVariable("id") int id){
        return jojoMapper.JojoAllInfo(id);
    }

    @GetMapping("/read")
    public void read(){
        ExcelReaderBuilder readBook= EasyExcel.read("C:\\Users\\Lenovo\\Desktop\\人员表.xlsx", User.class,new UserListener());
        ExcelReaderSheetBuilder sheet = readBook.sheet();
        sheet.doRead();
    }

    @GetMapping("/putOut")
    public void putOut(){

        List<Jojo> jojoList = jojoService.findAll();
        ExcelWriterBuilder writeWorkBook = EasyExcel.write("jojo列表.xlsx",Jojo.class);
        ExcelWriterSheetBuilder sheet = writeWorkBook.sheet();
        sheet.doWrite(jojoList);

    }

}
